package com.baize.mq.push;

import com.baize.mq.param.RabbitParam;
import com.baize.mq.pojo.RabbitRequest;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping(value = "publisher")
public class Publisher {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping(value = "send")
    public String send(
            @RequestBody RabbitRequest msg
    ) {
//        Message message = MessageBuilder
//                .withBody("消息".getBytes(StandardCharsets.UTF_8))
//                .setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT)
//                .build();
        for (int i = 0; i < 10; i++) {
            // 消息发送取消持久化
            rabbitTemplate.convertAndSend(
                    RabbitParam.EXCHANGE_NAME,
                    msg.getTopic(),
                    msg
            );
        }
        return "success";
    }
}
